<template>
	<view>
		<view style="padding:30rpx">
			<u-row>
				<u-col span="8">
					<u-input placeholder="请输入规格属性" border v-model="specDetailName"></u-input>
				</u-col>
				<u-col span="4">
					<u-button type="primary" size="medium" style="width: 100%;" @click="addspecDetailName">
						{{STATIC[static]}}
					</u-button>
				</u-col>
			</u-row>
		</view>
		<u-gap height="10" bg-color="#f2f2f2"></u-gap>
		<u-cell-group>
			<u-cell-item hover-class="none" :title="item.specDetailName" :arrow="false"
				v-for="(item,index) in specDetails" :key="index">
				<view right-icon>
					<u-icon name="trash" size="32" @click="delSpecDetailName(item)"></u-icon>
					<u-icon name="edit-pen" size="32" style="margin-left: 30rpx;" @click="editSpecDetailName(item)">
					</u-icon>
				</view>
			</u-cell-item>
		</u-cell-group>

		<u-modal v-model="show" content="此操作将永久删除该规格属性, 是否继续?" :zoom="false" show-cancel-button confirm-color="#1F6ECF"
			@confirm="confirm"></u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				specDetailName: '',
				productId: '',
				id: '',
				specDetails: [],
				specDetailsID: '',
				static: 0,
				STATIC: {
					0: '添加属性',
					1: '修改属性'
				}
			}
		},
		onLoad(option) {
			uni.setNavigationBarTitle({
				title: option.specName
			});
			this.productId = option.productId;
			this.id = option.id;
		},
		mounted() {
			this.getData();
		},
		methods: {
			getData() {
				this.$u.api.specList({
					product_id: this.productId
				}).then(res => {
					if (res.code == 0) {
						res.data.forEach(item => {
							if (item.id == this.id) {
								this.specDetails = item.specDetails;
							}
						});
					}
				})
			},
			addspecDetailName() {
				if(!this.specDetailName){
					this.$u.toast('属性名不能为空');
					return
				}
				if (this.static == 0) {
					this.$u.api.addSpecdetail({
						productSpecId: this.id,
						sortLevel: 0,
						specDetailName: this.specDetailName
					}).then(res => {
						if (res.code == 0) {
							this.$u.toast('新增成功');
							this.specDetailName = '';
							let pages = getCurrentPages();
							let currPage = pages[pages.length - 2];
							currPage.$vm.getData();
							this.getData();
						} else {
							this.$u.toast(res.msg)
						}
					});
				} else {
					this.$u.api.editSpecdetail({
						id: this.specDetailsID,
						productSpecId: this.id,
						sortLevel: 0,
						specDetailName: this.specDetailName
					}).then(res => {
						if (res.code == 0) {
							this.$u.toast('编辑成功');
							this.specDetailName = '';
							this.static = 0;
							let pages = getCurrentPages();
							let currPage = pages[pages.length - 2];
							currPage.$vm.getData();
							this.getData();
						} else {
							this.$u.toast(res.msg)
						}
					});

				}
			},
			delSpecDetailName(item) {
				this.show = true;
				this.specDetailsID = item.id;
			},
			confirm() {
				this.$u.api.delSpecdetail({
					id: this.specDetailsID,
				}).then(res => {
					if (res.code == 0) {
						this.$u.toast('删除成功');
						let pages = getCurrentPages();
						let currPage = pages[pages.length - 2];
						currPage.$vm.getData();
						this.getData();
					} else {
						this.$u.toast(res.msg)
					}
				});
			},
			editSpecDetailName(item) {
				this.specDetailName = item.specDetailName;
				this.specDetailsID = item.id;
				this.static = 1;
			}
		}
	}
</script>

<style>
</style>
